package com.novellotus.iov.carvice.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.bean.MqttListCarBean;
import com.novellotus.iov.carvice.bean.RxCarListBean;
import com.novellotus.iov.carvice.interfaces.AdapterBottomInterface;
import com.novellotus.iov.carvice.utils.MyUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Rich on 2017/7/26.
 * 车辆列表适配器
 */

public class CarListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements AdapterBottomInterface {

    ClickItemInterface clickItemInterface;
    List<RxCarListBean.RecordsBean> records;
    final int BOTTOM = 100;
    private boolean showBottom = false;

    public CarListAdapter(ClickItemInterface clickItemInterface, List<RxCarListBean.RecordsBean> records) {
        this.clickItemInterface = clickItemInterface;
        if (records == null) {
            this.records = new ArrayList<>();
        } else {
            this.records = records;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == records.size()) {
            return BOTTOM;
        } else {
            return 1;
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == BOTTOM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_bottom, parent, false);
            return new BottomHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_car_list_item, parent, false);
            return new CarHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (position == records.size()) {
            initBottom((BottomHolder) holder, position);
        } else {
            if (records.get(position) != null)
                initCarHolder((CarHolder) holder, position);
        }

    }

    private void initBottom(BottomHolder holder, int position) {
        holder.tv_bottom.setTextColor(Color.parseColor("#ffffff"));
        holder.tv_bottom.setAlpha(0.3f);
        holder.view_left.setAlpha(0.3f);
        holder.view_right.setAlpha(0.3f);
    }

    private void initCarHolder(CarHolder holder, final int position) {
        holder.tv_plate.setText(MyUtils.getPlateString(records.get(position).getPn()));
        holder.tv_vin.setText(records.get(position).getVin());
        holder.tv_model.setText(records.get(position).getName());
        holder.tv_statue.setVisibility(View.INVISIBLE);
        int type;
        if (position == records.size() - 1) {
            holder.view_hide_bottom.setVisibility(View.VISIBLE);
        } else {
            holder.view_hide_bottom.setVisibility(View.GONE);
        }
        //是否在线
        if (records.get(position).getOnlineStatus() == 0) {
            holder.view_line.setBackgroundResource(R.color.line_outline);
            holder.tv_plate.setAlpha(0.6f);
            holder.tv_vin.setAlpha(0.4f);
            type = 0;
        } else {
            holder.tv_plate.setAlpha(1f);
            holder.tv_vin.setAlpha(1f);
            holder.view_line.setBackgroundResource(R.color.line_online);
            type = 1;
            //是否充电
            switch (records.get(position).getChargeStatus()) {
                case 0:
                    break;
                case 1:
                    //充电
                case 2:
                    //行驶充电
                    holder.tv_statue.setVisibility(View.INVISIBLE);
                    holder.view_line.setBackgroundResource(R.color.line_charge);
                    type = 2;
                    break;
                case 3:
                    //未充电
                    break;
                case 4:
                    //充电完成
                    holder.view_line.setBackgroundResource(R.color.line_charge);
                    type = 2;
                    break;
            }
        }
        //是否报警
        switch (records.get(position).getAlarmStatus()) {
            case 1:
                //充电
                holder.tv_statue.setVisibility(View.VISIBLE);
                holder.tv_statue.setBackgroundResource(R.drawable.shape_one_alram);
                holder.tv_statue.setText("一级报警");
                type = 3;
                break;
            case 2:
                //行驶
                holder.tv_statue.setVisibility(View.VISIBLE);
                holder.tv_statue.setBackgroundResource(R.drawable.shape_two_alarm);
                holder.tv_statue.setText("二级报警");
                type = 3;
                break;
            case 3:
                //未充电
                holder.tv_statue.setVisibility(View.VISIBLE);
                holder.tv_statue.setBackgroundResource(R.drawable.shape_corners_alarm);
                holder.tv_statue.setText("三级报警");
                type = 3;
                break;
        }
        final int finalType = type;
        holder.rlt_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                clickItemInterface.openDetail(records.get(position).getPn(), records.get(position).getVin(), finalType);
            }
        });
    }


    /**
     * @param mqttListCarBean 刷新单个车辆
     */
    public void updateSingleCar(MqttListCarBean mqttListCarBean) {
        for (int i = 0; i < records.size(); i++) {
            if (records.get(i).getVin().equals(mqttListCarBean.getVin())) {
                MyUtils.logInformation("刷新adapter");
                records.get(i).setAlarmStatus(mqttListCarBean.getAlarmStatus());
                records.get(i).setChargeStatus(mqttListCarBean.getChargeStatus());
                records.get(i).setOnlineStatus(mqttListCarBean.getOnlineStatus());
                records.get(i).setVehicleStatus(mqttListCarBean.getVehicleStatus());
                notifyDataSetChanged();
            }
        }
    }

    @Override
    public int getItemCount() {
        if (showBottom) {
            return records.size() + 1;
        } else {
            return records.size();
        }
    }

    /**
     * @param records 刷新数据
     */
    public void update(List<RxCarListBean.RecordsBean> records) {
        this.records = records;
        notifyDataSetChanged();
    }

    /**
     * @param records 添加下拉时的
     */
    public void addData(List<RxCarListBean.RecordsBean> records) {
        this.records.addAll(records);
        notifyDataSetChanged();
    }

    @Override
    public void showBottomLayout() {
        showBottom = records.size() >= 5;

        notifyDataSetChanged();
    }

    @Override
    public void hideBottomLayout() {
        showBottom = false;
        notifyDataSetChanged();
    }

    class BottomHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.rlt_bottom)
        RelativeLayout rlt_bottom;
        @BindView(R.id.tv_bottom)
        TextView tv_bottom;
        @BindView(R.id.view_left)
        View view_left;
        @BindView(R.id.view_right)
        View view_right;

        BottomHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class CarHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_statue)
        TextView tv_statue;
        @BindView(R.id.tv_plate)
        TextView tv_plate;
        @BindView(R.id.tv_vin)
        TextView tv_vin;
        @BindView(R.id.tv_model)
        TextView tv_model;
        @BindView(R.id.rlt_item)
        RelativeLayout rlt_item;
        @BindView(R.id.view_hide)
        View view_hide_bottom;
        @BindView(R.id.view_line)
        View view_line;

        CarHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface ClickItemInterface {
        void openDetail(String plate, String vin, int type);
    }
}
